package com.landis.temple.mobile;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class TwitterLibrary extends ListActivity implements OnClickListener
{
    public final static int CREATE_STORY   = 0;
    public final static int CREATE_CHAPTER = 1;
    
    private TwitterStoryDatabase tsdb;
    private Cursor dbResults;
    private SimpleCursorAdapter adapter;
    
    // Filled with twitter user-name on credentials check
    private long myId = 0;
    
    /* Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
            
        View add_button = findViewById( R.id.create_button );
        add_button.setOnClickListener( this );
        
        tsdb = new TwitterStoryDatabase(this.getApplication().getBaseContext());
        tsdb = tsdb.open();
        
        updateList();
    }

    /*
     * Sets menu options for standard menu button
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate( R.menu.library_menu , menu);
        
        return true;
    }

    @Override
    protected void onDestroy() 
    {
        super.onDestroy();
        
        if( tsdb != null )
        {
            tsdb.close();
        }
    }

    @Override
    protected void onListItemClick( ListView list , View view , int position, long id) 
    {
        super.onListItemClick( list , view , position , id );   
        
        Log.d( "OnListItemClicked" , "id is " + id );
        
        Intent i = new Intent( this , TwitterStory.class );
        
        i.putExtra( "story_id" , id );
        i.putExtra( "author_id" , myId );
        
        startActivity( i );
    }
    
    private void updateList()
    {
        if( dbResults != null )
        {
            dbResults.close();
        }
        
        dbResults = tsdb.getAllStories();
        startManagingCursor(dbResults);
      
        if( dbResults != null )
        {
            // xml defined views
            int to[] = new int[] {  R.id.story_id           , 
                                    R.id.story_author       , 
                                    R.id.story_title        , 
                                    R.id.story_date_created };
            
            // columns 
            String[] from = dbResults.getColumnNames();
            
            adapter = new SimpleCursorAdapter( this           , 
                                               R.layout.books , 
                                               dbResults      , 
                                               from           , 
                                               to             );
            setListAdapter(adapter);
        }
    }

    @Override
    public void onClick(View add_button) 
    {        
        // if library then add story else add chapter
        // Used only in OnActivityResult to determine expected results

        Intent i = new Intent( this , CreateActivity.class );
        i.putExtra( "action" , CREATE_STORY );
        i.putExtra( "author_id" , myId );
        startActivityForResult( i , CREATE_STORY );
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        
        updateList();
    }
}